package cn.edu.xhu.library_management.area.service.impl;

import cn.edu.xhu.library_management.area.entity.LmBookRack;
import cn.edu.xhu.library_management.area.entity.LmBookRackArea;
import cn.edu.xhu.library_management.area.mapper.BookRackAreaMapper;
import cn.edu.xhu.library_management.area.mapper.BookRackMapper;
import cn.edu.xhu.library_management.area.service.BookRackAreaService;
import cn.edu.xhu.library_management.common.entity.ResultCode;
import cn.edu.xhu.library_management.common.exception.LibraryManagementException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2022 - 11 - 22
 * @Description:
 */
@Service
public class BookRackAreaServiceImpl implements BookRackAreaService {
    @Resource
    private BookRackAreaMapper bookRackAreaMapper;
    @Resource
    private BookRackMapper bookRackMapper;
    @Override
    public LmBookRackArea findRackAreaByComposeId(LmBookRackArea area) {
        return bookRackAreaMapper.findRackAreaByComposeId(area);
    }

    @Override
    public List<LmBookRackArea> findRackAreasByRackId(Integer rackId) {
        return bookRackAreaMapper.findRackAreasByRackId(rackId);
    }

    @Override
    public boolean addRackArea(LmBookRackArea rackArea) {
        // 判断书架id是否存在
        LmBookRack rack = bookRackMapper.findBookRackById(rackArea.getBookRackId());
        if (rack == null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"书架id不存在");
        }
        // 查询当前区域是否存在
        LmBookRackArea bookRackArea = bookRackAreaMapper.findRackAreaByComposeId(rackArea);
        if (bookRackArea != null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"书架区域已经存在");
        }
        return bookRackAreaMapper.addRackArea(rackArea) > 0;
    }

    @Override
    public boolean updateRackArea(LmBookRackArea rackArea) {
        // 查询区域id是否符合
        LmBookRackArea rackAreaById = bookRackAreaMapper.findRackAreaById(rackArea.getId());
        if (rackAreaById ==null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"书架区域不存在");
        }
        // 判断书架id是否存在
        LmBookRack rack = bookRackMapper.findBookRackById(rackArea.getBookRackId());
        if (rack == null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"书架id不存在");
        }
        // 查询当前区域是否存在
        LmBookRackArea bookRackArea = bookRackAreaMapper.findRackAreaByComposeId(rackArea);
        if (bookRackArea == null){
            throw new LibraryManagementException(ResultCode.OPERATION_ERROR,"书架区域不存在");
        }
        return bookRackAreaMapper.updateRackArea(rackArea) > 0;
    }

    @Override
    public boolean deleteRackAreaById(Integer rackAreaId) {
        return bookRackAreaMapper.deleteRackAreaById(rackAreaId) > 0;
    }
}
